package rbac

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"store/libs/helper"
	"store/logic"
	"store/models/rbac"
)

const (
	salt = "gin-store"
)

type AdminLogic struct {
	logic.RestLogic
	Ctx *gin.Context
}

type AdminInput struct {
	logic.XmlInput
	UserName     string `json:"userName" xml:"userName" form:"userName" field:"user_name" binding:"max=32" msg:"管理员登录名最大长度为32"`
	TrueName     string `json:"trueName" xml:"trueName" form:"trueName" field:"true_name" binding:"max=32" msg:"真实姓名最大长度为32"`
	Password     string `json:"password" xml:"password" form:"password" field:"password" binding:"max=32"`
	Id           uint64 `json:"id" xml:"id" form:"id" field:"id"`
	IsOn         uint8  `json:"isOn" xml:"isOn" form:"isOn" field:"is_on"`
	IsSuperAdmin uint8  `json:"isSuperAdmin" xml:"isSuperAdmin" form:"isSuperAdmin" field:"is_super_admin"`
}

type LoginInput struct {
	logic.XmlInput
	UserName string `json:"userName" xml:"userName" form:"userName" binding:"required,max=32" msg:"登录名必填，且长度最大为32"`
	Password string `json:"password" xml:"password" form:"password" binding:"required,max=32" msg:"密码必填，且长度最大为32"`
	Id       string `json:"id" xml:"id" form:"id" binding:"required" msg:"验证码必填"`
	Code     string `json:"code" xml:"code" form:"code" binding:"required" msg:"验证码必填"`
}

type LoginInfo struct {
	Ip        string `json:"ip"`
	Id        uint64 `json:"id"`
	AdminName string `json:"adminName"`
	LoginTime int64  `json:"loginTime"`
}

/**克隆logic,用于存放*gin.Context
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) Clone(ctx *gin.Context) logic.RestLogic {
	newLogic := *this
	newLogic.Ctx = ctx
	return &newLogic
}

/**解析http输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) LoadInput() (input interface{}, err error) {
	in := AdminInput{
		IsOn: 1,
	}
	err = this.Ctx.ShouldBind(&in)
	return in, err
}

/**解析http输入转化为update输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) LoadInput4Update(where map[string]interface{}) (input interface{}, version uint64, err error) {
	model, ok := this.View(where)
	if !ok {
		err = fmt.Errorf("not found")
		return
	}

	val := model.(rbac.Admin)
	value := AdminInfo2Input(&val)
	err = this.Ctx.ShouldBind(&value)
	input = value
	version = val.Version
	return
}

/**创建输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) NewCreateInput() interface{} {
	return AdminInput{
		IsOn: 1,
	}
}

/**
 * 数据库Model转Input
 */
/**数据库Model转Input
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminInfo2Input(info *rbac.Admin) AdminInput {
	return AdminInput{
		TrueName:     info.TrueName,
		UserName:     info.UserName,
		IsOn:         info.IsOn,
		IsSuperAdmin: info.IsSuperAdmin,
		Id:           info.Id,
	}
}

func encryptPassword(password string) string {
	return helper.Md5(helper.Md5(password+salt) + salt)
}

func DealPassword(info map[string]interface{}) {
	if info["password"].(string) != "" {
		info["password"] = encryptPassword(info["password"].(string))
		return
	}
	delete(info, "password")
}

func Login(userName string, password string) *rbac.Admin {
	admin := rbac.GetUserByUserName(userName)
	if admin.Id < 1 {
		return nil
	}

	if admin.Password != encryptPassword(password) {
		return nil
	}

	return admin
}

/**插入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) Add(input interface{}) (id uint64) {
	in := input.(*interface{})
	info := logic.Input2Map((*in).(AdminInput))
	DealPassword(info)
	return rbac.AdminCreate(this.Ctx, info)
}

/**删除
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) Delete(id uint64) (rows int64) {
	return rbac.AdminDelete(this.Ctx, id)
}

/**修改
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) Update(version uint64, input interface{}) (rows int64) {
	infoInput := input.(AdminInput)
	info := logic.Input2Map(infoInput)
	DealPassword(info)
	return rbac.AdminUpdate(this.Ctx, infoInput.Id, version, info)
}

/**启用禁用
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) UpOrDown(id uint64, value uint8) (rows int64) {
	return rbac.AdminUpOrDown(this.Ctx, id, value)
}

/**详情
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) View(where map[string]interface{}) (info interface{}, exist bool) {
	model := rbac.AdminInfo(this.Ctx, where)
	model.Password = ""
	return model, model.Id > 0
}

/**列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminLogic) Index(page int64, pageSize int64, where map[string]interface{}, orders []string) (pageData map[string]interface{}) {
	return rbac.AdminIndex(this.Ctx, page, pageSize, where, orders)
}
